clear all 
set more off

* big heat map with earnings profiles

* load
use "Transitions_3Qtr_DecileEarnings.dta", clear


* list of transitions
local t0list "MM MB MW1 MW2 MW3 MW4 MW5 MW6 MW7 MW8 MW9 MW10"
local t1list "M B W1 W2 W3 W4 W5 W6 W7 W8 W9 W10"
	
local rnn1 = 12
local rnn2 = 12


foreach t0 of local t0list {
	foreach t1 of local t1list {
		
		gen _hold = ytrate if trans == "`t0'`t1'"
		bys ccode year: egen T_`t0'`t1' = max(_hold)
		drop _hold
		
	}
}

duplicates drop ccode year, force
drop ytrate


* ------------ HEAT MAP 
* careful: everything below is in a particular order to 
* get the graphs looking nice.

	matrix heat2 = J(`rnn1',`rnn2',.)

	// The ordering of these needs to line up with the labels created below.
	local rn = 1
	foreach i of local t0list {

		local cn = 1
		foreach j of local t1list {
			reg T_`i'`j' lcgdp, cluster(ccode)
			matrix heat2[`rn',`cn'] = _b[lcgdp]
			
			local cn = `cn'+1
		}
			
		local rn = `rn' + 1
	}
	
	

	matrix rownames heat2 = U/N,U/N U/N,S U/N,W1 U/N,W2 U/N,W3 U/N,W4 U/N,W5 U/N,W6 U/N,W7 U/N,W8 U/N,W9 U/N,W10
	matrix colnames heat2 = U/N S W1 W2 W3 W4 W5 W6 W7 W8 W9 W10
	


	colorpalette HSV heat, n(50) reverse nograph
	*values(format(%9.2f)) colors(`r(p)',intensity(0.6))

	* HCL pinkgreen 

	#delimit ;
	heatplot heat2, 
	colors(HSV bluered2,intensity(1))
	cuts(-0.15(0.02)0.15)
	graphregion(color(white)) p(lcolor(black%10) lwidth(vthin))
	xtitle("To:") ytitle("From:") 
	xscale(alt lstyle(none)) xlabel(,tlength(0))
	yscale(lstyle(none)) ylabel(,tlength(0) nogrid)
	legend(off)
	name(heat_2)
	
	text(1 1 "+", size(small))
	text(2 2 "+", size(small))
	text(3 3 "+", size(small))
	text(4 4 "+", size(small))
	text(5 5 "+", size(small))
	text(6 6 "+", size(small))
	 text(7 7 "+", size(small))
	text(8 8 "+", size(small)) 
	text(9 9 "+", size(small))
	text(10 10 "+", size(small))
	text(11 11 "+", size(small))
	text(12 12 "+", size(small))
	;
	#delimit cr
	
	
	graph export "Figures/Figure5B.tif", replace
	graph export "Figures/pdf_figures/Figure5B.pdf", replace

